package edu.bit.water.service;

import edu.bit.water.dao.EntityWaterCountDao;
import edu.bit.water.dao.WasteWaterNewSub1Dao;
import edu.bit.water.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 标签表的业务层
 *
 * @author HJS
 */
@Service
public class WasteWaterNewSub1Service {

    @Autowired
    private WasteWaterNewSub1Dao wasteWaterNewSub1Dao;


    /**
     * 查询ENTITY_CLASSIFIED_INFO中的记录
     *
     * @return ENTITY_CLASSIFIED_INFO中的所有记录
     */
    public List<WasteWaterNewSub1> queryWasteWaterNewSub1(int last_id) {
        List<WasteWaterNewSub1> list = wasteWaterNewSub1Dao.select(last_id);
        return list;
    }

    /**
     * 更新记录
     *
     * @param entityWaterCount 标签记录实体
     * @return 成功返回1, 失败返回0
     */

    public Map<String, Object> updateWasteWaterNewSub1(WasteWaterNewSub1 wasteWaterNewSub1) {
        //metaDischargeMaxPermission.dateChange();
        Map<String, Object> result = new HashMap<>();
        int tmp = wasteWaterNewSub1Dao.update(wasteWaterNewSub1);
        if(tmp == 0) {
            result.put("status", "failed");
        }
        else {
            result.put("status", "success");
        }
        return result;
    }

    /**
     * 更新记录
     *
     * @param entityClassifiedStandard 标签记录实体
     * @return 成功返回1, 失败返回0
     */


    /**
     * 查询META_CLASSIFIED_STANDARD_INFO中的记录
     *
     * @return META_CLASSIFIED_STANDARD_INFO中的与standNum相应的所有记录
     */

    /**
     * 插入记录
     *
     * @param entityWaterCount 标签记录实体
     * @return 成功返回1, 失败返回0
     */
    public Map<String, Object> insertWasteWaterNewSub1(WasteWaterNewSub1 wasteWaterNewSub1) {
        //metaDischargeMaxPermission.dateChange();
        Map<String, Object> result = new HashMap<>();
        Integer num = wasteWaterNewSub1Dao.getMaxId();
//        System.out.println(num);
        if (num != null) {
            wasteWaterNewSub1.setId(num + 1);
        }
        else {
            wasteWaterNewSub1.setId(0);
        }
        int tmp = wasteWaterNewSub1Dao.insert(wasteWaterNewSub1);
        if (tmp == 1) {
            result.put("status", "success");
        }else {
            result.put("status", "failed");
        }
        return result;
    }

    /**
     * 删除记录
     *
     * @param ids 删除记录的标识符
     * @return 成功返回1, 失败返回0
     */
    public Map<String, Object> deleteWasteWaterNewSub1(List<String> wasteWaterNewSub1) {
        Map<String, Object> tmpMap = new HashMap<>();
        boolean flag = true;
        for (int i = 0; i < wasteWaterNewSub1.size(); i++) {
            int result = wasteWaterNewSub1Dao.delete(Integer.parseInt(wasteWaterNewSub1.get(i)));
            if(result == 0) {
                tmpMap.put(wasteWaterNewSub1.get(i), "failed");
                flag = false;
            }
            else {
                tmpMap.put(wasteWaterNewSub1.get(i), "success");
            }
        }
        if (flag) {
            tmpMap.put("status", "success");
        }
        else {
            tmpMap.put("status", "failed");
        }
        return tmpMap;
    }

    public List<MetaDischargeMaxPermission> getWaste() {
        List<MetaDischargeMaxPermission> list = wasteWaterNewSub1Dao.getWaste();
        return list;
    }

    public Map<String, String> labelUpdate(WasteWaterEva2 wasteWaterEva2) {
        Map<String, String> modelMap = new HashMap<>();

        int n = wasteWaterNewSub1Dao.labelUpdate(wasteWaterEva2);
        if (n == 1)
            modelMap.put("status", "success");
        else
            modelMap.put("status", "failed");
        return modelMap;


    }

}
